package com.cy.sc.controller;

import com.cy.sc.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * <br>@class: DeptConsumerController
 * <br>@description: 服务消费者
 * <br>@author: cy
 * <br>@date: 2020-04-01 15:17:58
 * <br>@version: 1.0
 * <br>
 * <br>@since: (版本) 作者 时间 注释
 */
@RestController
@RequestMapping("/consumer/dept")
public class DeptConsumerController {
    //与Service完全解耦
    @Autowired
    private RestTemplate restTemplate;

    //传统 实现是通过地址来获取
//    private static final String REST_URL_PREFIX = "http://localhost:8001";
    //Ribbon 实现是通过服务名来获取
    private static final String REST_URL_PREFIX = "http://SERVER-SERVICE-DEPT";

    @RequestMapping("/get/{id}")
    public Dept get(@PathVariable("id") long id) {
        Dept dept = restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
        return dept;
    }

    @RequestMapping("/list")
    public List<Dept> list() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
    }

    @RequestMapping("/add")
    public boolean add(Dept dept) {
        return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);
    }

}
